<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="司机会员号" prop="driverMid">
        <el-input
          v-model="queryParams.driverMid"
          placeholder="请输入司机会员号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车牌号" prop="plateNo">
        <el-input
          v-model="queryParams.plateNo"
          placeholder="请输入车牌号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="货物种类" prop="goodsType">
        <el-input
          v-model="queryParams.goodsType"
          placeholder="请输入货物种类"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="起始地" prop="startAddress">
        <el-input
          v-model="queryParams.startAddress"
          placeholder="请输入起始地"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="目的地" prop="endAddress">
        <el-input
          v-model="queryParams.endAddress"
          placeholder="请输入目的地"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="里程" prop="miliage">
        <el-input
          v-model="queryParams.miliage"
          placeholder="请输入里程"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="运输合同号" prop="transprotContractNumber">
        <el-input
          v-model="queryParams.transprotContractNumber"
          placeholder="请输入运输合同号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in dict.type.waybill_info_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="司机姓名" prop="driverName">
        <el-input
          v-model="queryParams.driverName"
          placeholder="请输入司机姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="运输方式" prop="transportMethods">
        <el-input
          v-model="queryParams.transportMethods"
          placeholder="请输入运输方式"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户运单号" prop="clientTransportNumber">
        <el-input
          v-model="queryParams.clientTransportNumber"
          placeholder="请输入客户运单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总价" prop="totalPrice">
        <el-input
          v-model="queryParams.totalPrice"
          placeholder="请输入总价"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发货人姓名" prop="consignorName">
        <el-input
          v-model="queryParams.consignorName"
          placeholder="请输入发货人姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发货人电话" prop="consignorTelephone">
        <el-input
          v-model="queryParams.consignorTelephone"
          placeholder="请输入发货人电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收货人姓名" prop="consigneeName">
        <el-input
          v-model="queryParams.consigneeName"
          placeholder="请输入收货人姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收货人电话" prop="consigneeTelephone">
        <el-input
          v-model="queryParams.consigneeTelephone"
          placeholder="请输入收货人电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发货时间" prop="deliverGoodsTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.deliverGoodsTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择发货时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="收货时间" prop="receivingTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.receivingTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择收货时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="货物重量" prop="goodsWeight">
        <el-input
          v-model="queryParams.goodsWeight"
          placeholder="请输入货物重量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['waybill:waybill:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['waybill:waybill:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['waybill:waybill:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['waybill:waybill:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="waybillList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="运单号" align="center" prop="waybillId" />
      <el-table-column label="司机会员号" align="center" prop="driverMid" />
      <el-table-column label="车牌号" align="center" prop="plateNo" />
      <el-table-column label="货物种类" align="center" prop="goodsType" />
      <el-table-column label="起始地" align="center" prop="startAddress" />
      <el-table-column label="目的地" align="center" prop="endAddress" />
      <el-table-column label="里程" align="center" prop="miliage" />
      <el-table-column label="运输合同号" align="center" prop="transprotContractNumber" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.waybill_info_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="司机姓名" align="center" prop="driverName" />
      <el-table-column label="运输方式" align="center" prop="transportMethods" />
      <el-table-column label="客户运单号" align="center" prop="clientTransportNumber" />
      <el-table-column label="总价" align="center" prop="totalPrice" />
      <el-table-column label="发货人姓名" align="center" prop="consignorName" />
      <el-table-column label="发货人电话" align="center" prop="consignorTelephone" />
      <el-table-column label="收货人姓名" align="center" prop="consigneeName" />
      <el-table-column label="收货人电话" align="center" prop="consigneeTelephone" />
      <el-table-column label="发货时间" align="center" prop="deliverGoodsTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deliverGoodsTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收货时间" align="center" prop="receivingTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.receivingTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="货物重量" align="center" prop="goodsWeight" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="toWaybillDetail(scope.row)"
            v-hasPermi="['waybill:waybill:edit']"
          >查看详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['waybill:waybill:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['waybill:waybill:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改运单详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="司机会员号" prop="driverMid">
          <el-input v-model="form.driverMid" placeholder="请输入司机会员号" />
        </el-form-item>
        <el-form-item label="车牌号" prop="plateNo">
          <el-input v-model="form.plateNo" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="货物种类" prop="goodsType">
          <el-input v-model="form.goodsType" placeholder="请输入货物种类" />
        </el-form-item>
        <el-form-item label="起始地" prop="startAddress">
          <el-input v-model="form.startAddress" placeholder="请输入起始地" />
        </el-form-item>
        <el-form-item label="目的地" prop="endAddress">
          <el-input v-model="form.endAddress" placeholder="请输入目的地" />
        </el-form-item>
        <el-form-item label="里程" prop="miliage">
          <el-input v-model="form.miliage" placeholder="请输入里程" />
        </el-form-item>
        <el-form-item label="运输合同号" prop="transprotContractNumber">
          <el-input v-model="form.transprotContractNumber" placeholder="请输入运输合同号" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.waybill_info_status"
              :key="dict.value"
:label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="司机姓名" prop="driverName">
          <el-input v-model="form.driverName" placeholder="请输入司机姓名" />
        </el-form-item>
        <el-form-item label="运输方式" prop="transportMethods">
          <el-input v-model="form.transportMethods" placeholder="请输入运输方式" />
        </el-form-item>
        <el-form-item label="客户运单号" prop="clientTransportNumber">
          <el-input v-model="form.clientTransportNumber" placeholder="请输入客户运单号" />
        </el-form-item>
        <el-form-item label="总价" prop="totalPrice">
          <el-input v-model="form.totalPrice" placeholder="请输入总价" />
        </el-form-item>
        <el-form-item label="发货人姓名" prop="consignorName">
          <el-input v-model="form.consignorName" placeholder="请输入发货人姓名" />
        </el-form-item>
        <el-form-item label="发货人电话" prop="consignorTelephone">
          <el-input v-model="form.consignorTelephone" placeholder="请输入发货人电话" />
        </el-form-item>
        <el-form-item label="收货人姓名" prop="consigneeName">
          <el-input v-model="form.consigneeName" placeholder="请输入收货人姓名" />
        </el-form-item>
        <el-form-item label="收货人电话" prop="consigneeTelephone">
          <el-input v-model="form.consigneeTelephone" placeholder="请输入收货人电话" />
        </el-form-item>
        <el-form-item label="发货时间" prop="deliverGoodsTime">
          <el-date-picker clearable size="small"
            v-model="form.deliverGoodsTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择发货时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="收货时间" prop="receivingTime">
          <el-date-picker clearable size="small"
            v-model="form.receivingTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择收货时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="货物重量" prop="goodsWeight">
          <el-input v-model="form.goodsWeight" placeholder="请输入货物重量" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWaybill, getWaybill, delWaybill, addWaybill, updateWaybill } from "@/api/business/waybill";

export default {
  name: "Waybill",
  dicts: ['waybill_info_status'],
  data() {
    return {
      // 是否显示运单表详情
      detail: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 运单详情表格数据
      waybillList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        driverMid: null,
        plateNo: null,
        goodsType: null,
        startAddress: null,
        endAddress: null,
        miliage: null,
        transprotContractNumber: null,
        status: null,
        driverName: null,
        transportMethods: null,
        clientTransportNumber: null,
        totalPrice: null,
        consignorName: null,
        consignorTelephone: null,
        consigneeName: null,
        consigneeTelephone: null,
        deliverGoodsTime: null,
        receivingTime: null,
        goodsWeight: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        driverMid: [
          { required: true, message: "司机会员号不能为空", trigger: "blur" }
        ],
        plateNo: [
          { required: true, message: "车牌号不能为空", trigger: "blur" }
        ],
        goodsType: [
          { required: true, message: "货物种类不能为空", trigger: "blur" }
        ],
        startAddress: [
          { required: true, message: "起始地不能为空", trigger: "blur" }
        ],
        endAddress: [
          { required: true, message: "目的地不能为空", trigger: "blur" }
        ],
        miliage: [
          { required: true, message: "里程不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ],
        driverName: [
          { required: true, message: "司机姓名不能为空", trigger: "blur" }
        ],
        transportMethods: [
          { required: true, message: "运输方式不能为空", trigger: "blur" }
        ],
        clientTransportNumber: [
          { required: true, message: "客户运单号不能为空", trigger: "blur" }
        ],
        totalPrice: [
          { required: true, message: "总价不能为空", trigger: "blur" }
        ],
        consignorName: [
          { required: true, message: "发货人姓名不能为空", trigger: "blur" }
        ],
        consignorTelephone: [
          { required: true, message: "发货人电话不能为空", trigger: "blur" }
        ],
        consigneeName: [
          { required: true, message: "收货人姓名不能为空", trigger: "blur" }
        ],
        consigneeTelephone: [
          { required: true, message: "收货人电话不能为空", trigger: "blur" }
        ],
        deliverGoodsTime: [
          { required: true, message: "发货时间不能为空", trigger: "blur" }
        ],
        receivingTime: [
          { required: true, message: "收货时间不能为空", trigger: "blur" }
        ],
        goodsWeight: [
          { required: true, message: "货物重量不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询运单详情列表 */
    getList() {
      this.loading = true;
      listWaybill(this.queryParams).then(response => {
        this.waybillList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        waybillId: null,
        driverMid: null,
        plateNo: null,
        goodsType: null,
        startAddress: null,
        endAddress: null,
        miliage: null,
        transprotContractNumber: null,
        status: "0",
        driverName: null,
        transportMethods: null,
        clientTransportNumber: null,
        totalPrice: null,
        consignorName: null,
        consignorTelephone: null,
        consigneeName: null,
        consigneeTelephone: null,
        deliverGoodsTime: null,
        receivingTime: null,
        goodsWeight: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.waybillId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加运单详情";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const waybillId = row.waybillId || this.ids
      getWaybill(waybillId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改运单详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.waybillId != null) {
            updateWaybill(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWaybill(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const waybillIds = row.waybillId || this.ids;
      this.$modal.confirm('是否确认删除运单详情编号为"' + waybillIds + '"的数据项？').then(function() {
        return delWaybill(waybillIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('waybill/waybill/export', {
        ...this.queryParams
      }, `waybill_${new Date().getTime()}.xlsx`)
    },
    toWaybillDetail(data) {
      this.$router.push({ path: "/manager/waybillDetail", query:{id:data.waybillId}});
    }
  }
};
</script>
